﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.DAO.Gateway
{
    public class VCGateway:Gateway
    {
        Vc aVc=new Vc();
        public void SaveVc(Vc aVc)
        {
            try
            {

                Connection.Open();
                string insertQuery =
                    string.Format(
                        "insert into t_Vc values (@Name,@Designation,@PresentAddress,@PermanentAddress,@ContactNo,@Email,@joiningDate)");
                Command.CommandText = insertQuery;
                Command.Parameters.Clear();
                Command.Parameters.Add("@Name", SqlDbType.VarChar);
                Command.Parameters["@Name"].Value = aVc.Name;
                 Command.Parameters.Add("@Designation", SqlDbType.VarChar);
                 Command.Parameters["@Designation"].Value = aVc.Designation;
                 Command.Parameters.Add("@PresentAddress", SqlDbType.VarChar);
                Command.Parameters["@PresentAddress"].Value = aVc.PresentAddress;
                Command.Parameters.Add("@PermanentAddress", SqlDbType.VarChar);
                Command.Parameters["@PermanentAddress"].Value = aVc.Permanentaddress;
                Command.Parameters.Add("@ContactNo", SqlDbType.VarChar);
                Command.Parameters["@ContactNo"].Value = aVc.ContactNo;
                Command.Parameters.Add("@Email", SqlDbType.VarChar);
                Command.Parameters["@Email"].Value = aVc.Email;
                Command.Parameters.Add("@JoiningDate", SqlDbType.VarChar);
                Command.Parameters["@JoiningDate"].Value = aVc.JoiningDate;
                Command.ExecuteNonQuery();



            }
                catch(FormatException exception)
                {
                    throw new Exception(exception.Message);
                }
            catch (Exception exception)
            {
                
                throw new Exception("Database Error",exception);
            }
            finally
            {
                if (Connection!=null && Connection.State==ConnectionState.Open)
                {
                    Connection.Close();
                }


            }
        }


        public Vc GetVCInformation()
        {
            try
            {
                Connection.Open();
                string selectQuery = string.Format("select * from t_Vc");
                Command.CommandText = selectQuery;
                SqlDataReader reader = Command.ExecuteReader();
                while (reader.Read())
                {
                    aVc.Name = reader["Name"].ToString();
                    aVc.Designation = reader["Designation"].ToString();
                    aVc.PresentAddress = reader["PresentAddress"].ToString();
                    aVc.Permanentaddress = reader["PermanentAddress"].ToString();
                    aVc.ContactNo = reader["ContactNo"].ToString();
                    aVc.Email = reader["Email"].ToString();
                }
            }
            catch (Exception exception)
            {
                
                throw new Exception("Sorry!System Error!Try Again!");
            }
            finally
            {
                if (Connection!=null && Connection.State==ConnectionState.Open)
                {
                    Connection.Close();
                }
            }
            return aVc;
        }
    }
}